import { http } from "@/utils/http";

type Result = {
  success: boolean;
  data: Array<any>;
};

type ResultTable = {
  success: boolean;
  data?: {
    /** 列表数据 */
    list: Array<any>;
    /** 总条目数 */
    total?: number;
    /** 每页显示条目个数 */
    pageSize?: number;
    /** 当前页数 */
    currentPage?: number;
  };
};

type ApiResponse<T = any> = {
  code: number;
  success: boolean;
  message: string;
  data: T;
};

// 广告代理商相关接口
export const getAdAgentList = (data?: object) => {
  const path = getnewUrl("/ad-agent", data, "get");
  return http.request<ApiResponse>("get", path);
};

export const createAdAgent = (data: object) => {
  return http.request<ApiResponse>("post", "/ad-agent", { data });
};

export const updateAdAgent = (data: object) => {
  const path = getnewUrl("/ad-agent", data, "put");
  return http.request<ApiResponse>("put", path, { data });
};

export const deleteAdAgent = (data: object) => {
  const path = getnewUrl("/ad-agent", data, "delete");
  return http.request<ApiResponse>("delete", path);
};

export const getAdAgentOptions = () => {
  return http.request<ApiResponse>("get", "/ad-agent-options");
};

// 广告账号相关接口
export const getAdAccountList = (data?: object) => {
  const path = getnewUrl("/ad-account", data, "get");
  return http.request<ApiResponse>("get", path);
};

export const createAdAccount = (data: object) => {
  return http.request<ApiResponse>("post", "/ad-account", { data });
};

export const updateAdAccount = (data: object) => {
  const path = getnewUrl("/ad-account", data, "put");
  return http.request<ApiResponse>("put", path, { data });
};

export const deleteAdAccount = (data: object) => {
  const path = getnewUrl("/ad-account", data, "delete");
  return http.request<ApiResponse>("delete", path);
};

export const exportAdAccount = (data?: object) => {
  return http.request<Blob>("post", "/ad-account/export", { 
    data,
    responseType: "blob"
  });
};

// 广告账号用户管理相关接口
export const getAdAccountUsers = (accountId: number) => {
  return http.request<ApiResponse>("get", `/ad-account/${accountId}/users`);
};

export const addAdAccountUsers = (accountId: number, data: { user_ids: number[] }) => {
  return http.request<ApiResponse>("post", `/ad-account/${accountId}/users`, { data });
};

export const removeAdAccountUsers = (accountId: number, data: { user_ids: number[] }) => {
  return http.request<ApiResponse>("delete", `/ad-account/${accountId}/users`, { data });
};

// 广告账号充值申请相关接口
export const getRechargeApplyList = (data?: object) => {
  const path = getnewUrl("/ad-recharge-apply", data, "get");
  return http.request<ApiResponse>("get", path);
};

export const createRechargeApply = (data: object) => {
  return http.request<ApiResponse>("post", "/ad-recharge-apply", { data });
};

export const createBatchRechargeApply = (data: { accounts: Array<{ account_id: number | string, amount: number | string }> }) => {
  return http.request<ApiResponse>("post", "/ad-recharge-apply/batch", { data });
};

export const updateRechargeApply = (data: object) => {
  const path = getnewUrl("/ad-recharge-apply", data, "put");
  return http.request<ApiResponse>("put", path, { data });
};

export const deleteRechargeApply = (data: object) => {
  const path = getnewUrl("/ad-recharge-apply", data, "delete");
  return http.request<ApiResponse>("delete", path);
};

export const getRechargeApplyDetail = (id: number) => {
  return http.request<ApiResponse>("get", `/ad-recharge-apply/${id}`);
};

export const processRechargeApply = (id: number, data: object) => {
  return http.request<ApiResponse>("post", `/ad-recharge-apply/${id}/process`, { data });
};

// 广告账号清零申请相关接口
export const getClearApplyList = (data?: object) => {
  const path = getnewUrl("/ad-clear-apply", data, "get");
  return http.request<ApiResponse>("get", path);
};

export const createClearApply = (data: object) => {
  return http.request<ApiResponse>("post", "/ad-clear-apply", { data });
};

export const updateClearApply = (data: object) => {
  const path = getnewUrl("/ad-clear-apply", data, "put");
  return http.request<ApiResponse>("put", path, { data });
};

export const deleteClearApply = (data: object) => {
  const path = getnewUrl("/ad-clear-apply", data, "delete");
  return http.request<ApiResponse>("delete", path);
};

export const getClearApplyDetail = (id: number) => {
  return http.request<ApiResponse>("get", `/ad-clear-apply/${id}`);
};

// 广告转化率统计相关接口
export const getConversionStats = (data?: object) => {
  const path = getnewUrl("/ad-conversion", data, "get");
  return http.request<ApiResponse>("get", path);
};

// 广告消耗记录相关接口
export const getConsumptionRecords = (data?: object) => {
  const path = getnewUrl("/ad-consumption", data, "get");
  return http.request<ApiResponse>("get", path);
};

export const createConsumptionRecord = (data: object) => {
  return http.request<ApiResponse>("post", "/ad-consumption", { data });
};

export const updateConsumptionRecord = (id: number, data: object) => {
  return http.request<ApiResponse>("put", `/ad-consumption/${id}`, { data });
};

export const deleteConsumptionRecord = (id: number) => {
  return http.request<ApiResponse>("delete", `/ad-consumption/${id}`);
};

export const getConsumptionRecordDetail = (id: number) => {
  return http.request<ApiResponse>("get", `/ad-consumption/${id}`);
};

// 广告代理商消耗统计相关接口
export const getAgentConsumptionStatistics = (data?: object) => {
  const path = getnewUrl("/ad-agent-consumption-statistics", data, "get");
  return http.request<ApiResponse>("get", path);
};

// 广告计划2统计相关接口（简化版，仅广告数据）
export const getAdPlan2Statistics = (data?: object) => {
  const path = getnewUrl("/ad-plan2-statistics", data, "get");
  return http.request<ApiResponse>("get", path);
};

// 待办事项统计接口
export const getTodoStats = () => {
  return http.request<ApiResponse>("get", "/todo-stats");
};

// 兼容旧代码
export const getAdAccount = (data?: object, method: string = "get") => {
  if (method === "get") {
    return getAdAccountList(data);
  } else if (method === "post") {
    return createAdAccount(data);
  } else if (method === "put") {
    return updateAdAccount(data);
  } else if (method === "delete") {
    return deleteAdAccount(data);
  }
};

/**
 * 拼装新的方法
 * @param path
 * @param data
 * @param method
 */
function getnewUrl(path: string, data?: any, method: string = "get"): string {
  if (method === "put" || method === "delete") {
    path = path + "/" + data.id;
  } else if (method === "get" && data !== undefined) {
    const removeEmpty = (obj: any) => Object.keys(obj)
      .filter(key => obj[key] !== undefined && obj[key] !== null && obj[key] !== "")
      .reduce((acc, key) => {
        acc[key] = obj[key];
        return acc;
      }, {});
    
    const cleanData = removeEmpty(data);
    const query = new URLSearchParams(cleanData).toString();
    path = query ? `${path}?${query}` : path;
  }

  return path;
}
